<?php

namespace App\Providers;

use App\Models\Article;
use App\Models\Comment;
use App\Observers\ArticleObserver;
use App\Observers\CommentObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
	 *
	 * 注册任何应用服务。
     *
     * @return void
     */
    public function register()
    {

    }

    /**
     * Bootstrap any application services.
     *
	 * 引导任何应用程序服务。
	 *
	 * 启动应用服务。
	 *
     * @return void
     */
    public function boot()
    {
		//文章模型的观察者
		Article::observe(ArticleObserver::class);
		//评论的观察者
		Comment::observe(CommentObserver::class);

		//监听sql到日志
		if(env('SQL_LOG')){
			\DB::listen(function ($query) {
				$tmp = str_replace('?', '"'.'%s'.'"', $query->sql);
				$qBindings = [];
				foreach ($query->bindings as $key => $value) {
					if (is_numeric($key)) {
						$qBindings[] = $value;
					} else {
						$tmp = str_replace(':'.$key, '"'.$value.'"', $tmp);
					}
				}
				$tmp = vsprintf($tmp, $qBindings);
				$tmp = str_replace("\\", "", $tmp);
				\Log::info(' execution time: '.$query->time.'ms; '.$tmp."\n\n\t");

			});
		}
    }
}
